package com.example.demo2.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo2.common.Result;
import com.example.demo2.config.ExcelHeadDataListener;
import com.example.demo2.config.ExcelHeadShopDataListener;
import com.example.demo2.entity.*;
import com.example.demo2.entity.DTO.*;
import com.example.demo2.service.AddressCityService;
import com.example.demo2.service.AddressCountyService;
import com.example.demo2.service.AddressProvinceService;
import com.example.demo2.service.ShopPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shopPlace")
public class ShopPlaceController {
    @Autowired
    private ShopPlaceService shopPlaceService;
    @Autowired
    private AddressProvinceService addressProvinceService;
    @Autowired
    private AddressCityService addressCityService;
    @Autowired
    private AddressCountyService addressCountyService;
    //根据code 分页查询所有商品
    @PostMapping("/findPageByCode")
    public Result findPageByCode(@RequestBody ShopPlaceDTO shopPlaceDTO) {
        return Result.success(shopPlaceService.findPageByCode(shopPlaceDTO));
    }
    //根据id 分页查询单独
    @GetMapping("/getShopInfo")
    public Result getShopInfo(@RequestParam Integer id) {
        LambdaQueryWrapper<ShopPlace> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShopPlace::getId,id);
        ShopPlace one = shopPlaceService.getOne(lqw);
        return Result.success(one);
    }
    //添加
    @PostMapping("/add")
    public Result addShop(@RequestBody ShopPlace shopPlace) {
        return Result.success(shopPlaceService.addShop(shopPlace));
    }
    //修改
    @PostMapping("/update")
    public Result updateShop(@RequestBody ShopPlace shopPlace) {
        shopPlace.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        if(shopPlace.getCountyCode() != null){

            LambdaQueryWrapper<AddressProvince> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(AddressProvince::getCode,shopPlace.getProvinceCode());
            AddressProvince addressProvince = addressProvinceService.getOne(lqw1);

            LambdaQueryWrapper<AddressCity> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(AddressCity::getCode,shopPlace.getCityCode());
            AddressCity addressCity = addressCityService.getOne(lqw2);

            LambdaQueryWrapper<AddressCounty> lqw3 = new LambdaQueryWrapper<>();
            lqw3.eq(AddressCounty::getCode,shopPlace.getCountyCode());
            AddressCounty addressCounty = addressCountyService.getOne(lqw3);

            shopPlace.setCompleteCity(addressProvince.getName()+addressCity.getName()+addressCounty.getName());
        }
        return Result.success(shopPlaceService.updateById(shopPlace));
    }
    //删除
    @PostMapping("/delete")
    public Result deleteShop(@RequestBody Integer id) {
        return Result.success(shopPlaceService.deleteShop(id));
    }
    //批量删除
    @PostMapping("/batch/deleteShop")
    public Result batchDeleteCity(@RequestBody List<Integer> ids) {
        return Result.success(shopPlaceService.removeBatchByIds(ids));
    }
    //批量导出v2.0 采用easyexcel方法
    @GetMapping("/batch/newExportAllInfo")
    public void newBatchExportAllInfo(HttpServletResponse response, @RequestParam String ids) throws Exception {
        String[] split = ids.split(",");
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (String each : split) {
            arrayList.add(Integer.parseInt(each));
        }
        List<ShopPlace> idslist = shopPlaceService.listByIds(arrayList);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        EasyExcel.write(response.getOutputStream(), ShopPlace.class).autoCloseStream(Boolean.FALSE).sheet("表1").doWrite(idslist);
    }
    // 下载导入模板
    @GetMapping("/template/ImportAllInfo")
    public void templateImportAllInfo(HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        ArrayList<TemplateImportDTO> list = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(), ShopTemplateImportDTO.class).autoCloseStream(Boolean.FALSE).sheet("用户信息").doWrite(list);
    }
    // 导入
    @PostMapping("/newImportAllInfo")
    public void newImportAllInfo(MultipartFile multipartFile) throws IOException {
        EasyExcel.read(multipartFile.getInputStream(), FieldShopDTO.class, new ExcelHeadShopDataListener(shopPlaceService)).sheet().doRead();
    }
}
